# apps.py
from django.apps import AppConfig
import requests
from django.conf import settings

class ChatllmConfig(AppConfig):
    default_auto_field = 'django.db.models.BigAutoField'
    name = 'chatLLM'

    def ready(self):
        # 在 ready 方法内部导入模型
        from .models import OllamaList

        ollama_ip = settings.OLLAMA_IP
        url = f'http://{ollama_ip}/api/tags'
        try:
            response = requests.get(url)
            if response.status_code == 200:
                models = response.json().get('models', [])
                for model in models:
                    models_name = model.get('name')
                    model_details = model
                    OllamaList.objects.update_or_create(
                        models_name=models_name,
                        defaults={'model_details': model_details}
                    )
        except Exception as e:
            print(f"Error fetching Ollama models: {e}")